package com.lib.common.shapeview.drawable;

import android.annotation.SuppressLint;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.DashPathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/12/4 19:56
 * @Annotation：
 */
public class ShapeDrawable extends Drawable {
    private ShapeState mShapeState;
    private final Paint mSolidPaint;
    private Rect mPadding;
    private final Paint mStrokePaint;
    private Paint mShadowPaint;
    private ColorFilter mColorFilter;
    private int mAlpha;
    private boolean mDither;
    private final Path mPath;
    private final RectF mRect;
    private final RectF mShadowRect;
    private final Path mShadowPath;
    private Paint mLayerPaint;
    private boolean mRectDirty;
    private boolean mMutated;
    private Path mRingPath;
    private boolean mPathDirty;
    private int mLayoutDirection;

    public ShapeDrawable() {
        this(new ShapeState());
    }

    public ShapeDrawable(ShapeState state) {
        this.mSolidPaint = new Paint(1);
        this.mStrokePaint = new Paint(1);
        this.mAlpha = 255;
        this.mPath = new Path();
        this.mRect = new RectF();
        this.mShadowRect = new RectF();
        this.mShadowPath = new Path();
        this.mPathDirty = true;
        this.mShapeState = state;
        this.initializeWithState(state);
        this.mRectDirty = true;
        this.mMutated = false;
        this.mStrokePaint.setStyle(Paint.Style.STROKE);
    }

    public ShapeState getShapeState() {
        return this.mShapeState;
    }

    public boolean getPadding(@NonNull Rect padding) {
        if (this.mPadding != null) {
            padding.set(this.mPadding);
            return true;
        } else {
            return super.getPadding(padding);
        }
    }

    public ShapeDrawable setPadding(int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) {
        return this.setPadding(new Rect(paddingLeft, paddingTop, paddingRight, paddingBottom));
    }

    public ShapeDrawable setPadding(Rect padding) {
        this.mPadding = padding;
        this.mPathDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setType(int shape) {
        this.mRingPath = null;
        this.mShapeState.setType(shape);
        this.mPathDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setWidth(int width) {
        this.mShapeState.width = width;
        this.mPathDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setHeight(int height) {
        this.mShapeState.height = height;
        this.mPathDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setRadius(float radius) {
        this.mShapeState.setCornerRadius(radius);
        this.mPathDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setRadius(float topLeftRadius, float topRightRadius, float bottomLeftRadius, float bottomRightRadius) {
        if (topLeftRadius == topRightRadius && topLeftRadius == bottomLeftRadius && topLeftRadius == bottomRightRadius) {
            return this.setRadius(topLeftRadius);
        } else {
            this.mShapeState.setCornerRadii(new float[]{topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius});
            this.mPathDirty = true;
            this.invalidateSelf();
            return this;
        }
    }

    public ShapeDrawable setSolidColor(@ColorInt int startColor, @ColorInt int endColor) {
        return this.setSolidColor(startColor, endColor);
    }

    public ShapeDrawable setSolidColor(@ColorInt int startColor, @ColorInt int centerColor, @ColorInt int endColor) {
        return this.setSolidColor(startColor, centerColor, endColor);
    }

    public ShapeDrawable setSolidColor(@ColorInt int... colors) {
        this.mShapeState.setSolidColor(colors);
        if (colors == null) {
            this.mSolidPaint.setColor(0);
        } else if (colors.length == 1) {
            this.mSolidPaint.setColor(colors[0]);
            this.mSolidPaint.clearShadowLayer();
        }

        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setSolidGradientType(int type) {
        this.mShapeState.setSolidGradientType(type);
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setSolidGradientOrientation(ShapeGradientOrientation orientation) {
        this.mShapeState.solidGradientOrientation = orientation;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setSolidGradientCenterX(float centerX) {
        this.mShapeState.solidCenterX = centerX;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setSolidGradientCenterY(float centerY) {
        this.mShapeState.solidCenterY = centerY;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setSolidGradientRadius(float radius) {
        this.mShapeState.gradientRadius = radius;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setStrokeColor(@ColorInt int startColor, @ColorInt int endColor) {
        return this.setStrokeColor(startColor, endColor);
    }

    public ShapeDrawable setStrokeColor(@ColorInt int startColor, @ColorInt int centerColor, @ColorInt int endColor) {
        return this.setStrokeColor(startColor, centerColor, endColor);
    }

    public ShapeDrawable setStrokeColor(@ColorInt int... colors) {
        this.mShapeState.setStrokeColor(colors);
        if (colors == null) {
            this.mStrokePaint.setColor(0);
        } else if (colors.length == 1) {
            this.mStrokePaint.setColor(colors[0]);
            this.mStrokePaint.clearShadowLayer();
        }

        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setStrokeGradientOrientation(ShapeGradientOrientation orientation) {
        this.mShapeState.strokeGradientOrientation = orientation;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setStrokeSize(int size) {
        this.mShapeState.setStrokeSize(size);
        this.mStrokePaint.setStrokeWidth((float)size);
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setStrokeDashSize(float dashSize) {
        this.mShapeState.strokeDashSize = dashSize;
        this.mStrokePaint.setPathEffect(dashSize > 0.0F ? new DashPathEffect(new float[]{dashSize, this.mShapeState.strokeDashGap}, 0.0F) : null);
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setStrokeDashGap(float dashGap) {
        this.mShapeState.strokeDashGap = dashGap;
        this.mStrokePaint.setPathEffect(this.mShapeState.strokeDashSize > 0.0F ? new DashPathEffect(new float[]{this.mShapeState.strokeDashSize, dashGap}, 0.0F) : null);
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setUseLevel(boolean useLevel) {
        this.mShapeState.useLevel = useLevel;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setShadowColor(@ColorInt int color) {
        this.mShapeState.shadowColor = color;
        this.mPathDirty = true;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setShadowSize(int size) {
        this.mShapeState.shadowSize = size;
        this.mPathDirty = true;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setShadowOffsetX(int offsetX) {
        this.mShapeState.shadowOffsetX = offsetX;
        this.mPathDirty = true;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setShadowOffsetY(int offsetY) {
        this.mShapeState.shadowOffsetY = offsetY;
        this.mPathDirty = true;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setRingInnerRadiusSize(int size) {
        this.mShapeState.ringInnerRadiusSize = size;
        this.mShapeState.ringInnerRadiusRatio = 0.0F;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setRingInnerRadiusRatio(float ratio) {
        this.mShapeState.ringInnerRadiusRatio = ratio;
        this.mShapeState.ringInnerRadiusSize = -1;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setRingThicknessSize(int size) {
        this.mShapeState.ringThicknessSize = size;
        this.mShapeState.ringThicknessRatio = 0.0F;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setRingThicknessRatio(float ratio) {
        this.mShapeState.ringThicknessRatio = ratio;
        this.mShapeState.ringThicknessSize = -1;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public ShapeDrawable setLineGravity(int lineGravity) {
        this.mShapeState.lineGravity = lineGravity;
        this.mRectDirty = true;
        this.invalidateSelf();
        return this;
    }

    public void intoBackground(View view) {
        if (this.mShapeState.strokeDashGap > 0.0F || this.mShapeState.shadowSize > 0) {
            view.setLayerType(View.LAYER_TYPE_SOFTWARE, (Paint)null);
        }

        view.setBackground(this);
        if (Build.VERSION.SDK_INT >= 17) {
            int layoutDirection = view.getLayoutDirection();
            if (Build.VERSION.SDK_INT >= 23) {
                this.setLayoutDirection(layoutDirection);
            }
        }

    }

    @SuppressLint({"WrongConstant"})
    public void draw(@NonNull Canvas canvas) {
        if (this.ensureValidRect()) {
            int prevFillAlpha = this.mSolidPaint.getAlpha();
            int prevStrokeAlpha = this.mStrokePaint.getAlpha();
            int currFillAlpha = this.modulateAlpha(prevFillAlpha);
            int currStrokeAlpha = this.modulateAlpha(prevStrokeAlpha);
            boolean haveShadow = this.mShapeState.shadowSize > 0;
            boolean haveStroke = currStrokeAlpha > 0 && this.mStrokePaint.getStrokeWidth() > 0.0F;
            boolean haveFill = currFillAlpha > 0;
            ShapeState st = this.mShapeState;
            boolean useLayer = haveStroke && haveFill && st.shapeType != 2 && currStrokeAlpha < 255 && (this.mAlpha < 255 || this.mColorFilter != null);
            float rad;
            if (useLayer) {
                if (this.mLayerPaint == null) {
                    this.mLayerPaint = new Paint();
                }

                this.mLayerPaint.setDither(this.mDither);
                this.mLayerPaint.setAlpha(this.mAlpha);
                this.mLayerPaint.setColorFilter(this.mColorFilter);
                rad = this.mStrokePaint.getStrokeWidth();
                ShapeDrawableUtils.saveCanvasLayer(canvas, this.mRect.left - rad, this.mRect.top - rad, this.mRect.right + rad, this.mRect.bottom + rad, this.mLayerPaint);
                this.mSolidPaint.setColorFilter((ColorFilter)null);
                this.mStrokePaint.setColorFilter((ColorFilter)null);
            } else {
                this.mSolidPaint.setAlpha(currFillAlpha);
                this.mSolidPaint.setDither(this.mDither);
                this.mSolidPaint.setColorFilter(this.mColorFilter);
                if (this.mColorFilter != null && !this.mShapeState.hasSolidColor) {
                    this.mSolidPaint.setColor(this.mAlpha << 24);
                }

                if (haveStroke) {
                    this.mStrokePaint.setAlpha(currStrokeAlpha);
                    this.mStrokePaint.setDither(this.mDither);
                    this.mStrokePaint.setColorFilter(this.mColorFilter);
                }
            }

            float startX;
            if (haveShadow) {
                if (this.mShadowPaint == null) {
                    this.mShadowPaint = new Paint();
                    this.mShadowPaint.setColor(0);
                    this.mShadowPaint.setStyle(Paint.Style.STROKE);
                }

                if (haveStroke) {
                    this.mShadowPaint.setStrokeWidth(this.mStrokePaint.getStrokeWidth());
                } else {
                    this.mShadowPaint.setStrokeWidth((float)this.mShapeState.shadowSize / 4.0F);
                }

                int shadowColor = this.mShapeState.shadowColor;
                if (ShapeDrawableUtils.setColorAlphaComponent(this.mShapeState.shadowColor, 255) == this.mShapeState.shadowColor) {
                    shadowColor = ShapeDrawableUtils.setColorAlphaComponent(this.mShapeState.shadowColor, 254);
                }

                this.mShadowPaint.setColor(shadowColor);
                if (Build.VERSION.SDK_INT >= 28) {
                    startX = (float)this.mShapeState.shadowSize / 2.0F;
                } else {
                    startX = (float)this.mShapeState.shadowSize / 3.0F;
                }

                this.mShadowPaint.setMaskFilter(new BlurMaskFilter(startX, BlurMaskFilter.Blur.NORMAL));
            } else if (this.mShadowPaint != null) {
                this.mShadowPaint.clearShadowLayer();
            }

            switch (st.shapeType) {
                case 0:
                    if (st.radiusArray != null) {
                        if (this.mPathDirty || this.mRectDirty) {
                            this.mPath.reset();
                            this.mPath.addRoundRect(this.mRect, st.radiusArray, Path.Direction.CW);
                            this.mPathDirty = this.mRectDirty = false;
                        }

                        if (haveShadow) {
                            this.mShadowPath.reset();
                            this.mShadowPath.addRoundRect(this.mShadowRect, st.radiusArray, Path.Direction.CW);
                            canvas.drawPath(this.mShadowPath, this.mShadowPaint);
                        }

                        canvas.drawPath(this.mPath, this.mSolidPaint);
                        if (haveStroke) {
                            canvas.drawPath(this.mPath, this.mStrokePaint);
                        }
                    } else if (st.radius > 0.0F) {
                        rad = st.radius;
                        startX = Math.min(this.mRect.width(), this.mRect.height()) * 0.5F;
                        if (rad > startX) {
                            rad = startX;
                        }

                        if (haveShadow) {
                            canvas.drawRoundRect(this.mShadowRect, rad, rad, this.mShadowPaint);
                        }

                        canvas.drawRoundRect(this.mRect, rad, rad, this.mSolidPaint);
                        if (haveStroke) {
                            canvas.drawRoundRect(this.mRect, rad, rad, this.mStrokePaint);
                        }
                    } else {
                        if (haveShadow) {
                            canvas.drawRect(this.mShadowRect, this.mShadowPaint);
                        }

                        if (this.mSolidPaint.getColor() != 0 || this.mColorFilter != null || this.mSolidPaint.getShader() != null) {
                            canvas.drawRect(this.mRect, this.mSolidPaint);
                        }

                        if (haveStroke) {
                            canvas.drawRect(this.mRect, this.mStrokePaint);
                        }
                    }
                    break;
                case 1:
                    if (haveShadow) {
                        canvas.drawOval(this.mShadowRect, this.mShadowPaint);
                    }

                    canvas.drawOval(this.mRect, this.mSolidPaint);
                    if (haveStroke) {
                        canvas.drawOval(this.mRect, this.mStrokePaint);
                    }
                    break;
                case 2:
                    RectF r = this.mRect;
                    Drawable.Callback callback = this.getCallback();
                    int lineGravity;
                    if (Build.VERSION.SDK_INT >= 17 && callback instanceof View) {
                        int layoutDirection = ((View)callback).getContext().getResources().getConfiguration().getLayoutDirection();
                        lineGravity = Gravity.getAbsoluteGravity(st.lineGravity, layoutDirection);
                    } else {
                        lineGravity = st.lineGravity;
                    }

                    float startY;
                    float stopX;
                    float stopY;
                    switch (lineGravity) {
                        case 3:
                            startX = 0.0F;
                            startY = 0.0F;
                            stopX = 0.0F;
                            stopY = r.bottom;
                            break;
                        case 5:
                            startX = r.right;
                            startY = 0.0F;
                            stopX = r.right;
                            stopY = r.bottom;
                            break;
                        case 17:
                        default:
                            float y = r.centerY();
                            startX = r.left;
                            startY = y;
                            stopX = r.right;
                            stopY = y;
                            break;
                        case 48:
                            startX = 0.0F;
                            startY = 0.0F;
                            stopX = r.right;
                            stopY = 0.0F;
                            break;
                        case 80:
                            startX = 0.0F;
                            startY = r.bottom;
                            stopX = r.right;
                            stopY = r.bottom;
                    }

                    if (haveShadow) {
                        canvas.drawLine(startX, startY, stopX, stopY, this.mShadowPaint);
                    }

                    canvas.drawLine(startX, startY, stopX, stopY, this.mStrokePaint);
                    break;
                case 3:
                    Path path = this.buildRing(st);
                    if (haveShadow) {
                        canvas.drawPath(path, this.mShadowPaint);
                    }

                    canvas.drawPath(path, this.mSolidPaint);
                    if (haveStroke) {
                        canvas.drawPath(path, this.mStrokePaint);
                    }
            }

            if (useLayer) {
                canvas.restore();
            } else {
                this.mSolidPaint.setAlpha(prevFillAlpha);
                if (haveStroke) {
                    this.mStrokePaint.setAlpha(prevStrokeAlpha);
                }
            }

        }
    }

    public boolean onLayoutDirectionChanged(int layoutDirection) {
        this.mLayoutDirection = layoutDirection;
        return this.mShapeState.shapeType == 2;
    }

    private int modulateAlpha(int alpha) {
        int scale = this.mAlpha + (this.mAlpha >> 7);
        return alpha * scale >> 8;
    }

    public int getChangingConfigurations() {
        return super.getChangingConfigurations() | this.mShapeState.changingConfigurations;
    }

    public void setAlpha(int alpha) {
        if (alpha != this.mAlpha) {
            this.mAlpha = alpha;
            this.invalidateSelf();
        }

    }

    public int getAlpha() {
        return this.mAlpha;
    }

    public void setDither(boolean dither) {
        if (dither != this.mDither) {
            this.mDither = dither;
            this.invalidateSelf();
        }

    }

    public void setColorFilter(ColorFilter cf) {
        if (cf != this.mColorFilter) {
            this.mColorFilter = cf;
            this.invalidateSelf();
        }

    }

    public int getOpacity() {
        return this.mShapeState.opaque ? PixelFormat.OPAQUE : PixelFormat.TRANSLUCENT;
    }

    protected void onBoundsChange(Rect r) {
        super.onBoundsChange(r);
        this.mRingPath = null;
        this.mPathDirty = true;
        this.mRectDirty = true;
    }

    protected boolean onLevelChange(int level) {
        super.onLevelChange(level);
        this.mRectDirty = true;
        this.mPathDirty = true;
        this.invalidateSelf();
        return true;
    }

    private Path buildRing(ShapeState shapeState) {
        if (this.mRingPath == null || shapeState.useLevelForShape && this.mPathDirty) {
            this.mPathDirty = false;
            float sweep = shapeState.useLevelForShape ? 360.0F * (float)this.getLevel() / 10000.0F : 360.0F;
            RectF bounds = new RectF(this.mRect);
            float x = bounds.width() / 2.0F;
            float y = bounds.height() / 2.0F;
            float thickness = shapeState.ringThicknessSize != -1 ? (float)shapeState.ringThicknessSize : bounds.width() / shapeState.ringThicknessRatio;
            float radius = shapeState.ringInnerRadiusSize != -1 ? (float)shapeState.ringInnerRadiusSize : bounds.width() / shapeState.ringInnerRadiusRatio;
            RectF innerBounds = new RectF(bounds);
            innerBounds.inset(x - radius, y - radius);
            bounds = new RectF(innerBounds);
            bounds.inset(-thickness, -thickness);
            if (this.mRingPath == null) {
                this.mRingPath = new Path();
            } else {
                this.mRingPath.reset();
            }

            Path ringPath = this.mRingPath;
            if (sweep < 360.0F && sweep > -360.0F) {
                ringPath.setFillType(Path.FillType.EVEN_ODD);
                ringPath.moveTo(x + radius, y);
                ringPath.lineTo(x + radius + thickness, y);
                ringPath.arcTo(bounds, 0.0F, sweep, false);
                ringPath.arcTo(innerBounds, sweep, -sweep, false);
                ringPath.close();
            } else {
                ringPath.addOval(bounds, Path.Direction.CW);
                ringPath.addOval(innerBounds, Path.Direction.CCW);
            }

            return ringPath;
        } else {
            return this.mRingPath;
        }
    }

    private boolean ensureValidRect() {
        if (!this.mRectDirty) {
            return !this.mRect.isEmpty();
        } else {
            this.mRectDirty = false;
            Rect bounds = this.getBounds();
            float inset = this.mStrokePaint.getStrokeWidth() * 0.5F;
            ShapeState st = this.mShapeState;
            float let = (float)bounds.left + inset + (float)this.mShapeState.shadowSize;
            float top = (float)bounds.top + inset + (float)this.mShapeState.shadowSize;
            float right = (float)bounds.right - inset - (float)this.mShapeState.shadowSize;
            float bottom = (float)bounds.bottom - inset - (float)this.mShapeState.shadowSize;
            this.mRect.set(let, top, right, bottom);
            float shadowLet;
            float shadowRight;
            if (this.mShapeState.shadowOffsetX > 0) {
                shadowLet = let + (float)this.mShapeState.shadowOffsetX;
                shadowRight = right;
            } else {
                shadowLet = let;
                shadowRight = right + (float)this.mShapeState.shadowOffsetX;
            }

            float shadowTop;
            float shadowBottom;
            if (this.mShapeState.shadowOffsetY > 0) {
                shadowTop = top + (float)this.mShapeState.shadowOffsetY;
                shadowBottom = bottom;
            } else {
                shadowTop = top;
                shadowBottom = bottom + (float)this.mShapeState.shadowOffsetY;
            }

            this.mShadowRect.set(shadowLet, shadowTop, shadowRight, shadowBottom);
            if (st.solidColors == null) {
                this.mSolidPaint.setShader((Shader)null);
            }

            if (st.strokeColors == null) {
                this.mStrokePaint.setShader((Shader)null);
            }

            if (st.solidColors != null) {
                RectF rect = this.mRect;
                float x0;
                float y0;
                switch (st.solidGradientType) {
                    case 0:
                        x0 = st.useLevel ? (float)this.getLevel() / 10000.0F : 1.0F;
                        float[] coordinate = ShapeDrawableUtils.computeLinearGradientCoordinate(this.mLayoutDirection, this.mRect, x0, st.solidGradientOrientation);
                        this.mSolidPaint.setShader(new LinearGradient(coordinate[0], coordinate[1], coordinate[2], coordinate[3], st.solidColors, st.positions, Shader.TileMode.CLAMP));
                        break;
                    case 1:
                        x0 = rect.left + (rect.right - rect.left) * st.solidCenterX;
                        y0 = rect.top + (rect.bottom - rect.top) * st.solidCenterY;
                        float level = st.useLevel ? (float)this.getLevel() / 10000.0F : 1.0F;
                        this.mSolidPaint.setShader(new RadialGradient(x0, y0, level * st.gradientRadius, st.solidColors, (float[])null, Shader.TileMode.CLAMP));
                        break;
                    case 2:
                        x0 = rect.left + (rect.right - rect.left) * st.solidCenterX;
                        y0 = rect.top + (rect.bottom - rect.top) * st.solidCenterY;
                        int[] tempSolidColors = st.solidColors;
                        float[] tempSolidPositions = null;
                        if (st.useLevel) {
                            tempSolidColors = st.tempSolidColors;
                            int length = st.solidColors.length;
                            if (tempSolidColors == null || tempSolidColors.length != length + 1) {
                                tempSolidColors = st.tempSolidColors = new int[length + 1];
                            }

                            System.arraycopy(st.solidColors, 0, tempSolidColors, 0, length);
                            tempSolidColors[length] = st.solidColors[length - 1];
                            tempSolidPositions = st.tempSolidPositions;
                            float fraction = 1.0F / (float)(length - 1);
                            if (tempSolidPositions == null || tempSolidPositions.length != length + 1) {
                                tempSolidPositions = st.tempSolidPositions = new float[length + 1];
                            }

                            float level1 = (float)this.getLevel() / 10000.0F;

                            for(int i = 0; i < length; ++i) {
                                tempSolidPositions[i] = (float)i * fraction * level1;
                            }

                            tempSolidPositions[length] = 1.0F;
                        }

                        this.mSolidPaint.setShader(new SweepGradient(x0, y0, tempSolidColors, tempSolidPositions));
                }

                if (!st.hasSolidColor) {
                    this.mSolidPaint.setColor(-16777216);
                }
            }

            if (st.strokeColors != null) {
                float level = st.useLevel ? (float)this.getLevel() / 10000.0F : 1.0F;
                float[] coordinate = ShapeDrawableUtils.computeLinearGradientCoordinate(this.mLayoutDirection, this.mRect, level, st.strokeGradientOrientation);
                this.mStrokePaint.setShader(new LinearGradient(coordinate[0], coordinate[1], coordinate[2], coordinate[3], st.strokeColors, st.positions, Shader.TileMode.CLAMP));
                if (!st.hasStrokeColor) {
                    this.mStrokePaint.setColor(-16777216);
                }
            }

            return !this.mRect.isEmpty();
        }
    }

    public int getIntrinsicWidth() {
        return this.mShapeState.width;
    }

    public int getIntrinsicHeight() {
        return this.mShapeState.height;
    }

    public Drawable.ConstantState getConstantState() {
        this.mShapeState.changingConfigurations = this.getChangingConfigurations();
        return this.mShapeState;
    }

    @NonNull
    public Drawable mutate() {
        if (!this.mMutated && super.mutate() == this) {
            this.mShapeState = new ShapeState(this.mShapeState);
            this.initializeWithState(this.mShapeState);
            this.mMutated = true;
        }

        return this;
    }

    private void initializeWithState(ShapeState state) {
        if (state.hasSolidColor) {
            this.mSolidPaint.setColor(state.solidColor);
        } else if (state.solidColors == null) {
            this.mSolidPaint.setColor(0);
        } else {
            this.mSolidPaint.setColor(-16777216);
        }

        this.mPadding = state.padding;
        if (state.strokeSize >= 0) {
            if (state.hasStrokeColor) {
                this.setStrokeColor(state.strokeColor);
            } else {
                this.setStrokeColor(state.strokeColors);
            }

            this.setStrokeSize(state.strokeSize);
            this.setStrokeDashSize(state.strokeDashSize);
            this.setStrokeDashGap(state.strokeDashGap);
        }
    }
}
